const { request } = require("../../services/request");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        myBannerBg: "../../assets/images/my-banner-bg.png",
        nameAuthIcon: "../../assets/images/change-user.png",
        currentTabKey: "",
        interrogationList: [],
        userId: JSON.parse(wx.getStorageSync("userInfo")).userId,
        tbAnsweUser: null,
        appMenuList: [],
        currentTabIndex: 0,
        visible: false,
        scrollHeight: 0,
        longPressId: '',
        visibleDelete: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let scrollInfo = {
            prevDistance: 0, //滚动条的距离（默认为0）
            screenHalfwidth: wx.getSystemInfoSync().windowWidth / 2,
        };
        this.data.scrollInfo = scrollInfo;
        this.initData();
    },
    initData() {
        // 资料卡
        request("POST", `/applet/answer/user/get/default/${this.data.userId}`)
            .then((res) => {
                this.setData({
                    tbAnsweUser: res.data.tbAnsweUser,
                    appMenuList: res.data.appMenuList,
                });
                // currentTabKey不能以数字开头，否则scroll-into-view不生效，前面加个字符m
                this.setData({
                    currentTabKey: res.data.appMenuList[0].menuId,
                });
                this.getInterrogationList();
            })
            .catch();
    },
    // 请求tab对话列表
    getInterrogationList() {
        const { userId, currentTabKey, tbAnsweUser } = this.data;
        const { answeUserId } = tbAnsweUser;
        let params = {
            userId,
            answeUserId,
            appMenuId: currentTabKey,
            pageSize: 1000,
            pageNum: 1,
        };
        request(
            "POST",
            "/applet/dialog/page/byappmenu?pageNum=1&pageSize=1000",
            params
        ).then((res) => {
            this.setData({ interrogationList: res.data.rows });
        });
    },
    // 处理点击tab
    onTabClick(e) {
        let id = e.currentTarget.dataset.key;
        let index = e.currentTarget.dataset.index;
        this.setData({
            currentTabKey: id,
            currentTabIndex: index,
        });
        this.getInterrogationList();
    },
    onTabscroll(e) {},
    handelLongPressDialog(e) {
        console.log('longpress e :>> ', e);
        const id = e.currentTarget.id
        this.setData({ longPressId: id, visibleDelete: true })
    },
    handleCancelDelete() {
        this.setData({ visibleDelete: false })
    },
    handleConfirmDelete() {
        this.setData({ visibleDelete: false })
        request(
            "POST",
            "/applet/dialog/deleteDig", { id: this.data.longPressId }
        ).then(res => {
            if (res.code == 200) {
                wx.showToast({
                    title: '已删除对话',
                    icon: 'success',
                })
                this.getInterrogationList()
            } else {
                wx.showToast({
                    title: '删除失败',
                    icon: 'none',
                })
                console.log('delete fail res :>> ', id, res);
            }
        })
    },
    handleSwithAccount() {
        wx.navigateTo({
            url: "/pages/user-list/index",
        });
    },
    handleCancel() {
        this.setData({
            visible: false,
        });
    },
    handleLogout() {
        this.setData({
            visible: true,
        });
    },
    handleConfirmLogout() {
        request("POST", `/logout`)
            .then((res) => {
                wx.clearStorageSync();
                wx.redirectTo({
                    url: "/pages/login/index",
                });
            })
            .catch();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        let that = this;
        wx.getSystemInfo({
            success: (res) => {
                let logicPixelRatio = 750 / res.safeArea.width;
                console.log("res", res);
                that.setData({
                    scrollHeight:
                        (res.safeArea.bottom - res.safeArea.top - 300) * logicPixelRatio,
                    scrollBottom: 150,
                });
            },
        });
        this.initData();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});